package cz.stv;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.DatagramPacket;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.LinkedTransferQueue;
import javax.swing.JTextField;
import javax.swing.JTextPane;

/**
 *
 * @author Martin Strouhal
 */
public class Listener implements ActionListener {

    private JTextPane history;
    private JTextField field;
    private LinkedTransferQueue<DatagramPacket> queue;

    public Listener(JTextPane h, JTextField f, LinkedTransferQueue<DatagramPacket> q) {
        history = h;
        field = f;
        queue = q;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (field.getText().compareTo("") != 0) {
            queue.add(new DatagramPacket(field.getText().getBytes(), field.getText().length()));
            if (history.getText().compareTo("") == 0) {
                history.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()).toString() + ": " + field.getText());
            } else {
                history.setText(history.getText() + "\n" + new SimpleDateFormat("HH:mm:ss").format(new Date()).toString() + ": " + field.getText());
            }
            field.setText(null);
        }
    }
}
